// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Chaos Emerald Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 13 : TYPE_CHAOSEMERALD

// Function declarations
reserve function ChaosEmerald_DebugDraw
reserve function ChaosEmerald_DebugSpawn
reserve function ChaosEmerald_OldDraw
reserve function ChaosEmerald_NewDraw

// Static Values
public value ChaosEmerald_value16 = 0;

// Tables

function ChaosEmerald_DebugDraw
	temp4 = object[0].value10
	temp4 -= ChaosEmerald_value16
	DrawSprite(temp4)
end function


function ChaosEmerald_DebugSpawn
	temp4 = object[0].value10
	temp4 -= ChaosEmerald_value16
	temp5 = 13
	CallFunction(DebugMode_SpawnObject)
	object[arrayPos0].drawOrder = 4
	object[arrayPos0].groupID = GROUP_BLOCKS
end function


event ObjectMain
	switch object.state
	case 1
		object.value0++
		if object.value0 == 32
			object.value0 = 0
			object.state++
		end if
		break
	case 2
		object.value0++
		if object.value0 == 2
			object[0].state = PlayerObject_Blank
			object.value0 = 0
			if object[0].xvel == 0
				if object[0].yvel == 0
					Sin(object[0].xvel, PlayerObject_value1)
					object[0].xvel *= 4
					Cos(object[0].yvel, PlayerObject_value1)
					object[0].yvel *= -4
				end if
			end if
			object[0].interaction = 0
			ResetObjectEntity(20, TypeName[Special Finish], 0, 0, 0)
			object[20].priority = PRIORITY_ACTIVE
			PlaySfx(SfxName[Exit SS], 0)
			object.state++
		end if
		break
	end switch
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object.state == 0
			BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object.state = 1
				object.value0 = 0
				object.drawOrder = 4
				CheckEqual(options.gameMode, 4)
				temp0 = checkResult
				CheckNotEqual(stage.actNum, 7)
				temp0 &= checkResult
				if temp0 == 0
					StopMusic()
					PlaySfx(SfxName[Emerald], 0)
				else
					PlaySfx(SfxName[Event], 0)
				end if		
				if stage.actNum < 8
					PlayerObject_value4 = 1
				end if
				stage.timeEnabled = 0
				stage.pauseEnabled = 0
				switch options.gameMode
					case 2
						timeAttack.result = stage.seconds
						timeAttack.result *= 100
						temp0 = stage.minutes
						temp0 *= 0x1770
						timeAttack.result += temp0
						timeAttack.result += stage.milliSeconds
						switch stage.playerListPos
							case 0
								arrayPos0 = 520
								break
							case 1
								arrayPos0 = 550
								break
							case 2
								arrayPos0 = 580
								break
							case 5
								arrayPos0 = 490
								break						
							case 6
								arrayPos0 = 3720
								break						
							case 7
								arrayPos0 = 3770
								break						
							case 8
								arrayPos0 = 3810
								break						
							case 9
								arrayPos0 = 3850
								break						
						end switch
						arraypos0 += stage.listPos
						if timeAttack.result < saveRAM[arrayPos0]
							PlaySfx(SfxName[Event], 0)
							saveRAM[arrayPos0] = timeAttack.result
						end if
						WriteSaveRAM()
						break
					case 4
						switch stage.actNum
							case 7
								timeAttack.result = stage.seconds
								timeAttack.result *= 100
								temp0 = stage.minutes
								temp0 *= 0x1770
								timeAttack.result += temp0
								timeAttack.result += stage.milliSeconds
								arrayPos0 = 651
								if timeAttack.result < saveRAM[arrayPos0]
									PlaySfx(SfxName[Event], 0)
									saveRAM[arrayPos0] = timeAttack.result
								end if
								WriteSaveRAM()
								break
							default
								recMilliSeconds = stage.milliSeconds
								recSeconds = stage.seconds
								recMinutes = stage.minutes
								break
						end switch
						break
				end switch
			end if
		end if
	next
end event

function ChaosEmerald_OldDraw
		object.value0++
		object.value0 &= 15
		if object.value0 < 8
			if stage.actNum < 9
				DrawSpriteXY(object.propertyValue, temp0, temp1)
			else
				DrawSpriteXY(12, temp0, temp1)
			end if
		else
			DrawSpriteXY(7, temp0, temp1)
		end if
end function

function ChaosEmerald_NewDraw
	object.inkEffect = INK_ALPHA
	object.animationTimer += 8
	object.animationTimer &= 511
	Sin(temp2, object.animationTimer)
	temp2 >>= 3
	temp2 += 190
	if temp2 > 255
		temp2 = 255
	end if
	object.alpha = temp2
	if stage.actNum < 9
		switch object.propertyValue
			case 2
				DrawSpriteXY(13, temp0, temp1)
				break
			case 3
				DrawSpriteXY(14, temp0, temp1)
				break
			default
				DrawSpriteXY(15, temp0, temp1)
				break		
		end switch
		DrawSpriteFX(object.propertyValue, FX_INK, temp0, temp1)
	else
		DrawSpriteXY(15, temp0, temp1)
		DrawSpriteFX(12, FX_INK, temp0, temp1)
	end if
end function

event ObjectDraw
	switch object.state
	case 0
		CallFunction(SpecialSetup_Function9)
		//CallFunction(ChaosEmerald_OldDraw)
		CallFunction(ChaosEmerald_NewDraw)
		break
	case 1
		CallFunction(SpecialSetup_Function9)
		object.frame = object.value0
		object.frame >>= 3
		object.frame += 8
		DrawSpriteXY(object.frame, temp0, temp1)
		break
	end switch
end event


event ObjectStartup
	LoadSpriteSheet("Special/Objects.gif")
	SpriteFrame(-12, -12, 24, 24, 176, 276) //Blue
	SpriteFrame(-12, -12, 24, 24, 201, 276) //Yellow
	SpriteFrame(-12, -12, 24, 24, 226, 276) //Magenta
	SpriteFrame(-12, -12, 24, 24, 251, 276) //Green
	SpriteFrame(-12, -12, 24, 24, 276, 276) //Red
	SpriteFrame(-12, -12, 24, 24, 301, 276) //Grey
	SpriteFrame(-12, -12, 24, 24, 401, 276) //Cyan
	
	SpriteFrame(-12, -12, 24, 24, 326, 276) //White BG
	
	//Ring Sparkles
	SpriteFrame(-12, -12, 24, 24, 401, 76) 
	SpriteFrame(-12, -12, 24, 24, 426, 76)
	SpriteFrame(-12, -12, 24, 24, 451, 76)
	SpriteFrame(-12, -12, 24, 24, 476, 76)

	SpriteFrame(-12, -12, 24, 24, 451, 276) //Orange
	SpriteFrame(-12, -12, 24, 24, 501, 276) //Purple BG
	SpriteFrame(-12, -12, 24, 24, 526, 276) //Green BG
	SpriteFrame(-12, -12, 24, 24, 551, 276) //Real White BG
	
	temp0 = 0
	ChaosEmerald_value16 = DebugMode_ObjCount
	while temp0 < 7
		SetTableValue(TypeName[Chaos Emerald], DebugMode_ObjCount, DebugMode_TypeTable)
		SetTableValue(ChaosEmerald_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(ChaosEmerald_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
	
	if options.gameMode == 4
		switch stage.actNum
			case 1
				recMilliSeconds = 0
				recSeconds = 0
				recMinutes = 0
				break
			default
				stage.milliSeconds = recMilliSeconds
				stage.seconds = recSeconds
				stage.minutes = recMinutes
				break
		end switch
	end if
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
LoadSpriteSheet("Special/Objects.gif")
	SpriteFrame(-12, -12, 24, 24, 176, 276)
	SpriteFrame(-12, -12, 24, 24, 201, 276)
	SpriteFrame(-12, -12, 24, 24, 226, 276)
	SpriteFrame(-12, -12, 24, 24, 251, 276)
	SpriteFrame(-12, -12, 24, 24, 276, 276)
	SpriteFrame(-12, -12, 24, 24, 301, 276)
	SpriteFrame(-12, -12, 24, 24, 401, 276)
	SpriteFrame(-12, -12, 24, 24, 326, 276)
	SpriteFrame(-12, -12, 24, 24, 401, 76)
	SpriteFrame(-12, -12, 24, 24, 426, 76)
	SpriteFrame(-12, -12, 24, 24, 451, 76)
	SpriteFrame(-12, -12, 24, 24, 476, 76)
end event
